home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 19 / macformat_19.iso / Shareware / Games / Star Flick / CT / Source / Main.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  7KB  |  318 lines

  1. /************************************************************************************
  2.  * Main.c
  3.  *
  4.  * CheeseToast by Jim Bumgardner
  5.  *
  6.  ************************************************************************************/
  7. #include "CToast.h"
  8. #include "ObjectWindow.h"
  9. #include <Traps.h>
  10.  
  11. #if __option(profile)            // 6/15 Optional profiling support
  12. #include <Console.h>
  13. #include <Profile.h>
  14. #endif
  15.  
  16. void     MyInitMacintosh(void);
  17. void    MyInitMultifinder(void);
  18. void     MyHandleEvent(void);
  19. void     MyDoEvent(EventRecord *theEvent);
  20. void    IdleObjects(EventRecord *theEvent);
  21. void    CheckEnvironment(void);
  22.  
  23. Boolean    gWNEImplemented;
  24. Boolean    gDoneFlag;
  25.  
  26. // Typical Macintosh Initialization Code
  27.  
  28.  
  29. // Main Entry Point
  30.  
  31. main()
  32. {
  33.     // Standard Mac Initialization
  34.     MyInitMacintosh();
  35.  
  36.     // Check if Multifinder (WaitNextEvent) is implemented
  37.     MyInitMultifinder();
  38.  
  39.     // Set up the menu bar
  40.     MySetUpMenus();
  41.  
  42.     // Check if this is the appropriate type of Macintosh
  43.     CheckEnvironment();
  44.  
  45.     // Open Graphics window
  46.     MyNewWindow();
  47.     BeginAttract();
  48.     
  49.     // Start Profiling
  50. #if __option(profile)            // 6/15 Optional profiling support
  51.     freopen("profile.log","w",stdout);        // If console isn't wanted
  52.     InitProfile(200,200);
  53.     _profile = 0;
  54.     // cecho2file("profile.log",false,stdout);    // If console is wanted
  55. #endif
  56.  
  57.     // main event loop
  58.     while (!gDoneFlag)            // Till the End of Time...
  59.         MyHandleEvent();        // Get an Event, do something about it
  60. #if __option(profile)
  61.     DumpProfile();
  62. #endif
  63.  
  64.     CleanUp();
  65. }
  66.  
  67. // Standard Macintosh Initialization
  68.  
  69. #define ExtraMasterBlocksNeeded    2
  70. void MyInitMacintosh(void)
  71. {
  72.     short    i;
  73.     InitGraf(&thePort);
  74.     InitFonts();
  75.     FlushEvents(everyEvent, 0);
  76.     InitWindows();
  77.     InitMenus();
  78.     TEInit();
  79.     InitDialogs(0L);
  80.     InitCursor();
  81.  
  82.     MaxApplZone();
  83.  
  84.     i = ExtraMasterBlocksNeeded;
  85.     while (i--)
  86.         MoreMasters();
  87. }
  88.  
  89. // Check if WaitNextEvent (Multifinder) is implemented on this Macintosh
  90.  
  91. void MyInitMultifinder(void)
  92. {
  93.     gWNEImplemented = (NGetTrapAddress(_WaitNextEvent, ToolTrap) != 
  94.                        NGetTrapAddress(_Unimplemented,ToolTrap));
  95. }
  96.  
  97. #define E_BadEnviron    10000
  98.  
  99. void CheckEnvironment(void)
  100. {
  101.     SysEnvRec    sEnv;
  102.     OSErr        oe;
  103.     static        StringPtr eStr[7] = 
  104.                 {"\pThis Mac is ancient history - sorry!",
  105.                  "\pCheeseToast needs Color Quickdraw - sorry!",
  106.                  "\pCheeseToast needs system 6.0.7 or greater - sorry!",
  107.                  "\pCheeseToast needs a 68020 or better - sorry!",
  108.                  "\pCheeseToast requires a 12\" monitor or larger - sorry!",
  109.                  "\pReset your monitor to 256 colors and try again.",
  110.                  "\pThe CT Resources file is missing!"};
  111.  
  112.     Boolean        errFlag = false;
  113.     char        eID = 0;
  114.  
  115.     oe = SysEnvirons(1,&sEnv);
  116.     errFlag = true;
  117.  
  118.     if (oe != noErr)
  119.         eID = 0;
  120.     else if (!sEnv.hasColorQD)
  121.         eID = 1;
  122.     else if (sEnv.systemVersion < 0x0607)
  123.         eID = 2;
  124.     else if (sEnv.processor < 3)
  125.         eID = 3;
  126.     else if (screenBits.bounds.bottom - screenBits.bounds.top < 384 ||
  127.              screenBits.bounds.right - screenBits.bounds.left < 512)
  128.         eID = 4;
  129.     else {
  130.         GDHandle    curDevice;
  131.         curDevice = GetGDevice();
  132.         if ((*curDevice)->gdPMap == NULL ||
  133.             (*(*curDevice)->gdPMap)->pixelSize != 8)
  134.             eID = 5;
  135.         else {
  136.             gResFile = OpenResFile("\pCT Resources");
  137.             if (gResFile == -1)
  138.                 eID = 6;
  139.             else
  140.                 errFlag = false;
  141.         }
  142.     }
  143.  
  144.     if (errFlag)
  145.     {
  146.         ParamText(eStr[eID],"\p","\p","\p");
  147.         StopAlert(E_BadEnviron, NULL);
  148.         ExitToShell();
  149.     }
  150. }
  151.  
  152. // The Main Event Dispatcher - this routine should be called repeatedly
  153.  
  154. void MyHandleEvent(void)
  155.  
  156. {
  157.     EventRecord    theEvent;
  158.     Boolean        ok;
  159.  
  160.     // If the more modern WaitNextEvent is implemented, use it 
  161.  
  162.     if (gWNEImplemented)
  163.         // We don't have to call SystemTask because WaitNextEvent calls it for us
  164.         // Get the next event
  165.         ok = WaitNextEvent(everyEvent,&theEvent,0L,NULL);
  166.  
  167.     else {
  168.         // we are running in (Single) Finder under system 6 or less
  169.         // Give Desk Accessories some processing time
  170.         SystemTask ();
  171.  
  172.         // Get the next event
  173.         ok = GetNextEvent (everyEvent, &theEvent);
  174.     }
  175.  
  176.     if (ok) {
  177.         // Handle the Event
  178.         MyDoEvent(&theEvent);
  179.     }
  180.     else
  181.         // Nothing happened, kick back...
  182.         IdleObjects(&theEvent);
  183. }
  184.  
  185.  
  186. void MyDoEvent(EventRecord *theEvent)
  187. {
  188.     short         windowCode;
  189.     WindowPtr    theWindow;
  190.  
  191.  
  192.     switch (theEvent->what) {
  193.     //
  194.     // Was the mouse button pressed?
  195.     case mouseDown:
  196.         // Find out where the mouse went down
  197.         windowCode = FindWindow (theEvent->where, &theWindow);
  198.  
  199.           switch (windowCode) {
  200.         case inSysWindow:     // Desk Accessory?
  201.             SystemClick (theEvent, theWindow);
  202.             break;
  203.             
  204.         case inMenuBar:        // Menu Bar?
  205.               MyAdjustMenus();
  206.             MyHandleMenu(MenuSelect(theEvent->where));
  207.             break;
  208.  
  209.         default:            // Cursor was inside our window
  210.             // If the window isn't in the front
  211.             if (theWindow != FrontWindow())
  212.                 // Make it so...
  213.                 SelectWindow(theWindow);
  214.             else {    
  215.  
  216.                 // Window is already in the front, handle the click
  217.                 switch (windowCode) {
  218.  
  219.                 case inContent:        // Content area?
  220.                     if (((WindowPeek) theWindow)->refCon == MyWindowID)
  221.                         ((ObjectWindowPtr) theWindow)->HandleClick(theWindow, theEvent->where);
  222.                     break;
  223.  
  224.                 case inDrag:        // Dragbar?
  225.                     {
  226.                         Rect    dragRect;
  227.                         dragRect = screenBits.bounds;
  228.                         // Handle the dragging of the window
  229.                         DragWindow(theWindow, theEvent->where, &dragRect);
  230.                     }
  231.                     break;
  232.  
  233.                  case inGoAway:                        // close box?
  234.                     // Only Grid Windows can be closed
  235.                       if (((WindowPeek) theWindow)->refCon == MyWindowID) {
  236.  
  237.                         // Handle the mouse tracking for the close box
  238.                           if (TrackGoAway(theWindow, theEvent->where))
  239.                             // If mouse is released inside the close box
  240.                             // Hide or close the window
  241.                             ((ObjectWindowPtr) theWindow)->Dispose(theWindow);
  242.                     }
  243.                       break;
  244.  
  245.                 case inGrow:                        // Grow box?
  246.                     {
  247.                         long    growResult;
  248.                         Rect    growRect;
  249.                         SetRect(&growRect,20,20,
  250.                                 screenBits.bounds.right,screenBits.bounds.bottom);
  251.  
  252.                         // Handle the mouse tracking for the resizing
  253.                         growResult = GrowWindow(theWindow,theEvent->where,&growRect);
  254.  
  255.                         // Change the size of the window
  256.                         SizeWindow(theWindow,LoWord(growResult),HiWord(growResult),true);
  257.  
  258.                         // Redraw the window
  259.                         SetPort(theWindow);
  260.                           InvalRect(&theWindow->portRect);
  261.                     }
  262.                     break;
  263.                 }
  264.             }
  265.             break;
  266.         }
  267.         break;
  268.         
  269.     // Was a key pressed?
  270.     case keyUp:
  271.     case keyDown: 
  272.     case autoKey:
  273.         // Was the cmd-key being held down?  If so, process menu bar short cuts.
  274.         if ((theEvent->modifiers & cmdKey) != 0) {
  275.           MyAdjustMenus();
  276.           MyHandleMenu(MenuKey((char) (theEvent->message & charCodeMask)));
  277.         }
  278.         else {
  279.             theWindow = FrontWindow();
  280.             if (((WindowPeek) theWindow)->refCon == MyWindowID &&
  281.                 ((ObjectWindowPtr) theWindow)->ProcessKey != NULL)
  282.                 ((ObjectWindowPtr) theWindow)->ProcessKey(theEvent);
  283.         }
  284.         break;
  285.  
  286.     // Does a window need to be redrawn?
  287.     case updateEvt:
  288.         theWindow = (WindowPtr) theEvent->message;
  289.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  290.             ((ObjectWindowPtr) theWindow)->Update(theWindow);
  291.         break;
  292.  
  293.     // Has a window been activated or deactivated?
  294.     case activateEvt:
  295.         theWindow = (WindowPtr) theEvent->message;
  296.  
  297.         // Force it to be redrawn
  298.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  299.             ((ObjectWindowPtr) theWindow)->Activate(theWindow);
  300.  
  301.         break;
  302.     }
  303. }        
  304.  
  305. // Do Idle Time Processing
  306.  
  307. void IdleObjects(EventRecord *theEvent)
  308. {
  309.     WindowPeek    theWin;
  310.     theWin = (WindowPeek) FrontWindow();
  311.     while (theWin) {
  312.         if (theWin->refCon == MyWindowID &&
  313.             ((ObjectWindowPtr) theWin)->Idle)
  314.             ((ObjectWindowPtr) theWin)->Idle((WindowPtr) theWin,theEvent);
  315.         theWin = theWin->nextWindow;
  316.     }
  317. }
  318.